package com.yycwxt.modules.admin.Return;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.yycwxt.common.BaseService;
import com.yycwxt.domain.OperateFee;
import com.yycwxt.domain.Return;
import com.yycwxt.modules.admin.operatefee.OperateFeeService;
import lombok.SneakyThrows;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ReturnService extends BaseService<ReturnMapper, Return> {
    @SneakyThrows
    public boolean importFileToDB(boolean deleteHistory, MultipartFile file) {
        if (deleteHistory) {
            this.remove(null);
        }
        EasyExcel.read(file.getInputStream(), Return.class, new ReadListener<Return>() {

            private static final int BATCH_SIZE = 1000;
            private List<Return> ls = Lists.newArrayList();

            @Override
            public void invoke(Return data, AnalysisContext context) {
                //System.err.println(data);
                ls.add(data);
                if (ls.size() >= BATCH_SIZE) {
                    ReturnService.this.saveBatch(ls);
                    ls.clear();
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                ReturnService.this.saveBatch(ls);
            }
        }).sheet().doRead();
        return true;
    }

    private ReturnMapper returnMapper;

    @Autowired
    public ReturnService(ReturnMapper returnMapper){this.returnMapper = returnMapper;}

    public List<Map<String, Object>> getreturn(int zq){
        return returnMapper.getreturn(zq);
    }

    // 下载源文件
    public List<String> getDpdkUrl(){
        return returnMapper.getDpdkUrl();
    }
}
